import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, {constantRoutes, resetRouter} from '@/router'
import store from "@/store";

const getDefaultState = () => {
  return {
    token: getToken(),
    name: sessionStorage.getItem('name'),
    sysName:sessionStorage.getItem('sysName'),
    roles:JSON.parse(sessionStorage.getItem('roles')),
    menus:JSON.parse(sessionStorage.getItem('menus')),
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    sessionStorage.setItem('name',name)
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_SYS_NAME: (state, sysName) => {
    sessionStorage.setItem('sysName',sysName)
    state.sysName = sysName
  },
  SET_ROLES: (state, role) => {
    sessionStorage.setItem('roles',JSON.stringify(role))
    state.roles = role
  },
  SET_MENUS: (state, menus) => {
    sessionStorage.setItem('menus',JSON.stringify(menus))
    state.menus = menus
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    // const { username, password } = userInfo

    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        console.log(response)
        // const { data } = response
        commit('SET_TOKEN', response.token)
        setToken(response.token)

        commit('SET_ROLES',response.roles)
        commit('SET_MENUS',response.menus)
        commit('SET_SYS_NAME',response.sysName)

        commit('SET_NAME',response.user.username)

        let menus = store.state.user.menus

        // constantRoutes.forEach(r=>{
        //   // console.log(r)
        //   // if(r.path==='/sys'){
        //   //   return true
        //   // }
        //
        //   if(r.children){
        //     r.children = r.children.filter(item=>{
        //       if(item.path==='/dashboard'){
        //         return true
        //       }
        //       if(menus.indexOf(item.path.replaceAll("/", ""))!==-1){
        //         return true
        //       }
        //       return false
        //       // console.log(menus.indexOf(item.path.replaceAll("/", ""))!==-1);
        //       // console.log(menus)
        //       // console.log(item)
        //     })
        //     // return false
        //   }

        // })
        console.log(constantRoutes)
        router.addRoutes(constantRoutes)


        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

